home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1994 December / PSL Monthly Shareware CD-ROM (Public Software Library)(December 1994).bin / prgmming / dos / c1 / finddev.c < prev    next >
C/C++ Source or Header  |  1993-04-26  |  4KB  |  140 lines

  1. // Program : FINDDEV.C
  2. // Author  : Eric Woodruff,  CIS ID: 72134,1150
  3. // Updated : 04/26/93 10:23:56
  4. // Note    : Copyright 1992-93, Eric Woodruff, All rights reserved
  5. // Compiler: Borland C++ 3.1
  6. //
  7. // Walk the device driver chain looking for the specified device driver name.
  8. // This is useful in AUTOEXEC.BAT for basing the execution of a set of
  9. // commands on whether or not a device driver is loaded.  I wrote it to
  10. // see if a network driver loaded successfully or not.
  11. // Should work with DOS 2.x and up although I've only tested it on 3.x and up
  12. // and DR DOS 6.0.
  13. //
  14. // Syntax:  FINDDEV /Nname /V
  15. //          where "name" in /Nname is the device driver name to locate and
  16. //          and /V is the optional verbose switch that will list names
  17. //          as they are located.  If neither /N nor /V are used, the
  18. //          default is to list all device driver names.
  19. //
  20. // Example FINDNAME.BAT
  21. //  @ECHO OFF
  22. //
  23. //  REM %1 = /Nname where name is device name to find.
  24. //  REM %2 = /V optional to list all names as they are found.
  25. //  FINDDEV %1 %2
  26. //  IF ERRORLEVEL 3 GOTO NOTFOUND
  27. //  IF ERRORLEVEL 2 GOTO FOUND
  28. //
  29. //  ECHO Error in parameters: FINDDEV %1 %2
  30. //  GOTO END
  31. //
  32. //  :FOUND
  33. //  ECHO FINDDEV %1 %2 : Found
  34. //  GOTO END
  35. //
  36. //  :NOTFOUND
  37. //  ECHO FINDDEV %1 %2 : Not Found
  38. //
  39. //  :END
  40. //
  41.  
  42. #include <ctype.h>
  43. #include <dos.h>
  44. #include <stdio.h>
  45. #include <stdlib.h>
  46. #include <string.h>
  47.  
  48. #define  ERROR_EXIT     1
  49. #define  FOUND_NAME     2
  50. #define  NAME_NOT_FOUND 3
  51.  
  52. struct DeviceHeader {
  53.     struct DeviceHeader far *NextDev;   // Points to next device driver header.
  54.     short  Attribute;
  55.     short  StrategyPtr;
  56.     short  InterruptPtr;
  57.     char   Name_Unit[8];
  58. };
  59.  
  60. void main(int argc, char *argv[])
  61. {
  62.     struct DeviceHeader far *Device;
  63.     char  DName[9], *SearchName = NULL, SearchText[9] = "";
  64.     short Verbose = 0;
  65.  
  66.     while(--argc > 0)
  67.         if(argv[argc][0] == '/' || argv[argc][0] == '-')
  68.             switch(toupper(argv[argc][1]))
  69.             {
  70.                 case 'N':
  71.                     SearchName = &argv[argc][2];    // Name to find.
  72.                     if(strlen(SearchName) > 8)
  73.                     {
  74.                         puts("Search name is greater than 8 characters");
  75.                         exit(ERROR_EXIT);
  76.                     }
  77.                     // Pad with spaces to match device drive name format.
  78.                     sprintf(SearchText, "%-8s", SearchName);
  79.                     break;
  80.  
  81.                 case 'V':
  82.                     Verbose = 1;    // Print name as they are found.
  83.                     break;
  84.  
  85.                 default:
  86.                     puts("Format: FINDDEV /Nname [/V]");
  87.                     exit(ERROR_EXIT);
  88.             }
  89.         else
  90.         {
  91.             puts("Format: FINDDEV /Nname [/V]");
  92.             exit(ERROR_EXIT);
  93.         }
  94.  
  95.     if(!SearchName)
  96.         Verbose = 1;        // No search name, list all devices found.
  97.  
  98. asm {
  99.     mov     ax, 0x5200      // Get first device driver pointer.
  100.     int     0x21
  101.     cmp     _osmajor, 3     // DOS 3 or greater?
  102.     jge     short Is30orUp
  103.     add     bx, 0x17        // Point to first byte of device NUL:.
  104.     jmp     short WalkList
  105.     }
  106.  
  107. Is30orUp:
  108.  
  109. asm add     bx, 0x22        // It's a little further down for DOS 3.x and up.
  110.  
  111. WalkList:
  112. asm {
  113.     mov     word ptr &Device + 2, es        // Set first pointer.
  114.     mov     word ptr &Device,     bx
  115.     }
  116.  
  117.     // While not end of list (Segment = 0xFFFF), look for the device name.
  118.     for( ; FP_SEG(Device) != 0xFFFF; Device = Device->NextDev)
  119.     {
  120.         // Device names are 8 character, space padded strings (NO NULL).
  121.         // Drives are represented by their number as an ASCII character.
  122.         if(Device->Name_Unit[0] < 32)
  123.             sprintf(DName, "Drive %d", Device->Name_Unit[0]);
  124.         else
  125. #if defined(__TINY__) || defined(__SMALL__) || defined(__MEDIUM__)
  126.             _fstrncpy((char far *)DName, Device->Name_Unit, 8);
  127. #else
  128.             strncpy(DName, Device->Name_Unit, 8);
  129. #endif
  130.         DName[8] = '\x0';
  131.  
  132.         if(Verbose)
  133.             printf("%s\n", DName);
  134.  
  135.         if(!stricmp(SearchText, DName))
  136.             exit(FOUND_NAME);       // Exit as soon as it is found.
  137.     }
  138.     exit(NAME_NOT_FOUND);       // Wasn't there.
  139. }
  140.